package com.whp.yky.yky.controller;


import com.whp.yky.yky.entity.poentity.Document;
import com.whp.yky.yky.service.DocumentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "档案接口")
@CrossOrigin
@RestController
public class DocumentController {

    @Autowired
    private DocumentService documentService;


    @ApiOperation(value="获取档案信息", notes="根据用户名获取档案全部信息")
    @PostMapping("/findAllByDocumentName")
    public Object getDocumentInfoByID(@RequestParam String document_name){

        if (document_name!=null&&!"".equals(document_name)){

            return documentService.getDocumentInfoByDocumentName(document_name);

        }else{

            return "获取信息失败";

        }


    }

    @ApiOperation(value="上传档案", notes="上传档案")
    @PostMapping("/insertDocument")
    public String insertDocumentInfo(@RequestBody Map<String,String> documentMap){


        Document document=new Document();
        document.setId(documentMap.get("id"));
        document.setDocument_name(documentMap.get("document_name"));
        document.setDocument_type(documentMap.get("document_type"));
        document.setDocument_state(documentMap.get("document_state"));
        document.setVersion(documentMap.get("version"));
        document.setPerson(documentMap.get("person"));
        document.setAdress(documentMap.get("adress"));

        boolean result= documentService.insertDocumentInfo(document);
        if (result){
            return "success";
        }else {
            return "fail";
        }
    }

    @DeleteMapping("/deletDocumentByID/{id}")
    @ApiOperation(value="删除档案", notes="根据id删除档案")
    public String deletDocumentByID(@PathVariable("id") String id){
        boolean result = documentService.deleteDocumentById(id);
        if (result){
            return "sucess";
        }else{
            return "false";
        }
    }

    @ApiOperation(value="修改档案信息", notes="修改档案信息")
    @PostMapping("/updateDocumentInfo")
    public String updateDocumentInfo(@RequestBody Map<String,String> documentMap){


        Document document=new Document();
        document.setId(documentMap.get("id"));
        document.setDocument_name(documentMap.get("document_name"));
        document.setDocument_type(documentMap.get("document_type"));
        document.setDocument_state(documentMap.get("document_state"));
        document.setVersion(documentMap.get("version"));
        document.setPerson(documentMap.get("person"));
        document.setAdress(documentMap.get("adress"));

        boolean result= documentService.updateDocumentInfo(document);
        if (result){
            return "success";
        }else {
            return "fail";
        }
    }

    @ApiOperation(value="获取所有档案信息", notes="获取所有档案信息")
    @GetMapping("/DocumentInfo")
    public List<Document> findAll(){
        return documentService.findAll();
    }

}
